package servlet.vehicle;

import bean.pojo.ParkingRecord;
import bean.pojo.User;
import bean.pojo.Vehicle;
import com.alibaba.fastjson2.JSONObject;
import commons.baseServlet.BaseServlet;
import commons.format.NumberFormat;
import commons.r.R;
import service.ParkingRecordService;
import service.UsersService;
import service.VehicleService;
import service.impl.ParkingRecordServiceImpl;
import service.impl.UsersServiceImpl;
import service.impl.VehicleServiceImpl;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.List;

/**
 * servlet.vehicle
 * User: 坚持
 * Date: 2024/12/19 14:14
 * motto: 树欲静而风不止
 * Description:
 * Version: V1.0
 */
@WebServlet(name="ParkingRecordManagementServlet",value = "/parkingRecord/*")
public class ParkingRecordManagementServlet extends BaseServlet {
    public R add(HttpServletRequest request){
        try {
            BufferedReader bufferedReader=request.getReader();
            String req_data=bufferedReader.readLine();
            ParkingRecord parkingRecord = JSONObject.parseObject(req_data,ParkingRecord.class);
            ParkingRecordService parkingRecordService = new ParkingRecordServiceImpl();
            Boolean add=parkingRecordService.add(parkingRecord);
            return add?R.ok():R.error("添加失败");
        } catch (IOException e) {
            e.printStackTrace();
        }
        return R.error("出错");
    }
    public R deleteById(HttpServletRequest request){
        Integer parkingRecordId = NumberFormat.stringToInteger(request.getParameter("parkingRecordId"));
        ParkingRecordService parkingRecordService = new ParkingRecordServiceImpl();
        Boolean delete=parkingRecordService.deleteById(parkingRecordId);
        return delete?R.ok():R.error("删除失败");
    }
    public R update(HttpServletRequest request){
        try {
            BufferedReader bufferedReader = request.getReader();
            String req_data = bufferedReader.readLine();
            ParkingRecord parkingRecord = JSONObject.parseObject(req_data,ParkingRecord.class);
            ParkingRecordService parkingRecordService = new ParkingRecordServiceImpl();
            Boolean update = parkingRecordService.update(parkingRecord);
            return update?R.ok():R.error("修改失败");
        } catch (IOException e) {
            e.printStackTrace();
        }
        return R.error("出错");
    }
    public R findBy(HttpServletRequest request){
        Integer page = NumberFormat.stringToInteger(request.getParameter("page"));
        Integer limit = NumberFormat.stringToInteger(request.getParameter("limit"));
        Integer vehicleId = NumberFormat.stringToInteger(request.getParameter("vehicleId"));
        String startParkingTime = request.getParameter("startParkingTime");
        String endParkingTime = request.getParameter("endParkingTime");
        ParkingRecordService parkingRecordService = new ParkingRecordServiceImpl();
        Long total = parkingRecordService.findByTotal(vehicleId,startParkingTime,endParkingTime);
        List<ParkingRecord> list=null;
        if(total>0) {
            //当前页码的数据
            list = parkingRecordService.findBy(page, limit,vehicleId,startParkingTime,endParkingTime);
        }
        return R.ok().addData("total",total).addData("list",list);
    }
}
